package cn.huiyunche.admin.tms.controller;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.huiyunche.admin.service.AdminCAccntBillService;
import cn.huiyunche.base.service.enums.UserTypeEnum;
import cn.huiyunche.base.service.interfaces.CAccntBillService;
import cn.huiyunche.base.service.interfaces.CAccntTurnOverService;
import cn.huiyunche.base.service.vo.CBillAdminVo;
import cn.huiyunche.base.service.vo.CTurnoverAdminVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;

/**
 * 用户账户余额 列表
 * @author lm
 *
 */
@Controller
@RequestMapping("/balance")
public class BalanceController {
	
	private static final Logger log = LoggerFactory.getLogger(BalanceController.class);
	
	@Autowired
	private CAccntBillService cAccntBillService = null;
	
	@Autowired
	private AdminCAccntBillService adminCAccntBillService = null;
	
	@Autowired
	private CAccntTurnOverService cAccntTurnOverService = null;
	
	private CAccntTurnOverService getCAccntTurnOverService(){
		return this.cAccntTurnOverService;
	}
	
	private CAccntBillService getCAccntBillService(){
		return this.cAccntBillService;
	}
	private AdminCAccntBillService getAdminCAccntBillService(){
		return this.adminCAccntBillService;
	}
	
	/**
	 * 人送项目 司机账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/billlist", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getDriverBills(PageVo page, CBillAdminVo bill){
		log.info("select send by driver bills controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCAccntBillService().getBills(page, bill, UserTypeEnum.SEND_DRIVER.getValue()));
		return result;
	}
	
	/**
	 * 人送项目 司机账单
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> getDetails(PageVo page, CTurnoverAdminVo cTurnover){
		log.info("select send by driver bill deatils controller.");
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCAccntTurnOverService().getsendByDriverDetails(page, cTurnover));
		return result;
	}

	/**
	 * 人送项目 按照账单结算
	 * @param page
	 * @param cTurnover
	 * @return
	 */
	@RequestMapping(value = "/settlement", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> settlement(Long id){
		log.info("send by driver bill settlement by id params id : {}.", id);
		return this.getCAccntBillService().settlementById(id);
	}

	@RequestMapping(value = "/exportexcel", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> exportExcel(CBillAdminVo bill){
		log.info("exportExcel params,{}", JSONObject.toJSONString(bill));
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		Map<String, Object> map = new HashedMap();
		String[] titleName = new String[]{"账单编号","账户ID","账户名称","电话","账单金额","开始日期","结束日期","账期","状态","创建时间"};
		map.put("title", titleName);
		map.put("name", "司机提现申请列表");
		map.put("list", this.getCAccntBillService().exportDriverWithdrawalApplication(bill, UserTypeEnum.SEND_DRIVER.getValue(), titleName.length));
		result.setData(map);
		return result;
	}
	
	/**
	 * 人送项目 奖罚
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/rewardandpunish", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> rewardandpunish(Long id,BigDecimal money,String remark,String type){
		if(StringUtils.isBlank(remark)){
			log.error("driver bill rewardandpunish remark {}:",remark);
		}
		if(StringUtils.isBlank(type)){
			log.error("driver bill rewardandpunish type {}:",type);
		}
		log.info("driver bill rewardandpunish id : {},money :{} ", id,money);
		return this.getAdminCAccntBillService().rewardandpunish(id,money,remark,type);
	}

}
